/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lifecraft;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;

/**
 *
 * @author oem
 */
public class MenuGame {
    
        private LifeCraft lc;        
        private String[] options = new String[5];
        private int[] options_x = new int[options.length];
        private int[] options_xs = new int[options.length];
        private int[] options_y = new int[options.length];
        public int choice;
        public int ch_index;
        public boolean first_animation;
        private boolean last_animation;
        public int y_anim; 
        public boolean exit;
        
        public boolean menu; 
        MenuGame(LifeCraft loaded){
            lc = loaded;    
            options[0] = "CONTINUE";
            options[1] = "FULLSCREEN";
            options[2] = "SAVE";
            options[3] = "LOAD";
            options[4] = "EXIT GAME";
            for (int i=0;i<options.length;i++){
                options_x[i]=(lc.R_WIDTH/2)-(int)(24*(options[i].length()/2.0));
                options_xs[i]=(lc.R_WIDTH/2)-(int)(26*(options[i].length()/2.0));
                options_y[i]=(i+1)*(int)((0.75*lc.R_HEIGHT)/5.0);
            }
            ch_index = Time.get().add(10);
        }
        
        private void continueGame(){
            Keyboard.destroy();
            Mouse.destroy();
            try{
                Keyboard.create();
                Mouse.create();
            }
            catch(LWJGLException e) {
                System.out.println("Unable to create Mouse or Keyboard"+ e);
            }


            Time.get().pauseGame();
            menu=false;
            lc.world.First.update_need= true;
            lc.world.a.update_need= true;
            lc.world.b.update_need= true;
            lc.world.c.update_need= true;
        }
        private void enter(){
            switch(choice){
            case 0:
                first_animation = false;
                last_animation = true;
                break;
            case 1:
                System.out.println("F");
                lc.fullscreen = (lc.fullscreen?false:true);
                lc.setDisplayMode(1024,768,lc.fullscreen); 
                break;
            case 4:
                exit = true;
                
                
                break;
            }
        }
        
        private void input(){
         
        if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
	    System.out.println("SPACE KEY IS DOWN");
        }
        for(int i=0;i<options.length;i++){
            if(lc.mouseBlock(options_x[i], options_y[i], 24*(options[i].length()), 50)){
                choice = i;
            }
        }
        
        
        while(Mouse.next()|| Keyboard.next()){
            if (Mouse.getEventButtonState()) {
	        if (Mouse.getEventButton() == 0) {
		    if(lc.mouseBlock(options_x[choice], options_y[choice], 24*(options[choice].length()), 50)){
                        enter();
                    }
                    
		}
            }
            if (Keyboard.getEventKeyState()) {
	        switch(Keyboard.getEventKey()){
                
                    case Keyboard.KEY_F:
                        System.out.println("F");
                        lc.fullscreen = (lc.fullscreen?false:true);
                        lc.setDisplayMode(1024,768,lc.fullscreen);   
                        break;
                    case Keyboard.KEY_DOWN:
                        choice += (choice==options.length-1)?0:1;
                        break;
                    case Keyboard.KEY_UP:
                        choice -= (choice==0)?0:1;
                        break;
                    case Keyboard.KEY_RETURN:
                        enter();
                        break;
                    case Keyboard.KEY_ESCAPE:
                        first_animation = false;
                        last_animation = true;
                        break;
                }
            }
        }
    }
     public void menuGame(){
        
         GL11.glDisable(GL11.GL_TEXTURE_2D);
            GL11.glColor4f(0.75f, 1.f, 1f, 0.75f);
            GL11.glBegin(GL11.GL_QUADS);
                GL11.glVertex2f(lc.R_WIDTH/4, y_anim);
                GL11.glVertex2f(3*(lc.R_WIDTH/4),y_anim);
                GL11.glVertex2f(3*(lc.R_WIDTH/4),lc.R_HEIGHT-y_anim);   
                GL11.glVertex2f(lc.R_WIDTH/4,lc.R_HEIGHT-y_anim);
            GL11.glEnd();
            GL11.glEnable(GL11.GL_TEXTURE_2D);
            if(first_animation||last_animation){
                if(Time.get().check(ch_index)){
                    if(first_animation){
                        y_anim -= 16;
                        if(y_anim <= lc.R_HEIGHT/8){
                            y_anim = lc.R_HEIGHT/8;
                            first_animation = false;  
                        }
                    }
                    else{
                        y_anim += 16;
                        if(y_anim >= lc.R_HEIGHT/2){
                            y_anim = lc.R_HEIGHT/2;
                            last_animation = false;
                            continueGame();
                        }
                    }
                }
            }
            else{
              for(int i=0;i<options.length;i++){
                if (i==choice) FontBase.get().f2.write(options_xs[i], options_y[i], options[i], Color.blue);   
                else  FontBase.get().f1.write(options_x[i], options_y[i], options[i], Color.yellow);   
              }
            }
            
            

         input();
         
         
     }
    
}
